package cn.szq.concurrent.thread.threadLocal;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;

/**
 * @ClassName: ThreadLocalDemo3
 * @Description: 所有的线程都共用一个 simpleDateFormat 对象 线程不安全
 * @Author: szq
 * @Date: 2021-10-18 08:20:35
 */
public class ThreadLocalDemo3 {

    public static ExecutorService threadPool = Executors.newFixedThreadPool(16);
    static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");

    public static void main(String[] args) throws InterruptedException {
        Set<String> result = new HashSet<>();
        IntStream.rangeClosed(1, 1000).forEach(t->{
            threadPool.submit(() -> {
                String date = new ThreadLocalDemo3().date(t);
                System.out.println(date);
                result.add(date);
            });
        });
        threadPool.shutdown();
        Thread.sleep(1000);
        System.out.println(result.size() == 1000);
    }

    private String date(int i) {
        Date date = new Date(i * 1000);
        return simpleDateFormat.format(date);
    }
}
